﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace APOrigin.Cryptography
{


}

namespace Crypto
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                threedanalect.AnacleWebService ws = new threedanalect.AnacleWebService();
                threedanalect.ServiceTenant[] tenants = ws.GetTenantUsers();
                foreach (threedanalect.ServiceTenant tenant in tenants)
                {
                    Console.WriteLine(tenant.Email);
                }


                System.Random rnd = new Random();
                double temp = rnd.Next(40, 76) + rnd.NextDouble();


















                //web.queensland.dev.EchoWebService ws = new web.queensland.dev.EchoWebService();
                //web.queensland.dev. Message msg = ws.Echo("adsd");

                //string original = "Here is some data to encrypt!";

                //// Create a new instance of the AesManaged
                //// class.  This generates a new key and initialization 
                //// vector (IV).

                //System.Security.Cryptography.AesManaged crypto = new System.Security.Cryptography.AesManaged();
                //string base64Key = Convert.ToBase64String(crypto.Key);
                //string base64IV = Convert.ToBase64String(crypto.IV);
                //Console.WriteLine("base64Key:[{0}]", base64Key);
                //Console.WriteLine("base64IV:[{0}]", base64IV);

                //AesCryptography crypt = new AesCryptography(base64Key, base64IV);

                //byte[] cipherText = crypt.Encrypt(original);
                //Console.WriteLine("CipherText:[{0}]", Convert.ToBase64String(cipherText));

                //string plainText = crypt.Decrypt(cipherText);
                //Console.WriteLine("PlainText:[{0}]", plainText);




                //using (System.Security.Cryptography.AesManaged myAes = new System.Security.Cryptography.AesManaged())
                //{

                //    // Encrypt the string to an array of bytes.
                //    byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);

                //    // Decrypt the bytes to a string.
                //    string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);

                //    //Display the original data and the decrypted data.
                //    Console.WriteLine("Original:   {0}", original);
                //    Console.WriteLine("Round Trip: {0}", roundtrip);
                //}

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }

        static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;
            // Create an AesManaged object
            // with the specified key and IV.
            using (System.Security.Cryptography.AesManaged aesAlg = new System.Security.Cryptography.AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                System.Security.Cryptography.ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream())
                {
                    using (System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream.
            return encrypted;

        }

        static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an AesManaged object
            // with the specified key and IV.
            using (System.Security.Cryptography.AesManaged aesAlg = new System.Security.Cryptography.AesManaged())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decrytor to perform the stream transform.
                System.Security.Cryptography.ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherText))
                {
                    using (System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                    {
                        using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }
    }

    public class AesCryptography
    {
        public byte[] _key { get; set; }
        public byte[] _iv { get; set; }

        #region constructors
        public AesCryptography()
        {
            System.Security.Cryptography.AesManaged crypto = new System.Security.Cryptography.AesManaged();
            this._key = crypto.Key;
            this._iv = crypto.IV;
        }
        public AesCryptography(string base64Key, string base64IV)
        {
            this._key = Convert.FromBase64String(base64Key);
            this._iv = Convert.FromBase64String(base64IV);
        }
        #endregion constructors

        public byte[] Encrypt(string plainText)
        {
            byte[] cipherText = null;

            using (System.Security.Cryptography.AesManaged crypto = new System.Security.Cryptography.AesManaged())
            {
                //crypto.Key = this._key;
                //crypto.Key = this._iv;

                System.Security.Cryptography.ICryptoTransform encryptor = crypto.CreateEncryptor(this._key, this._iv);

                // Create the streams used for encryption.
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(
                        ms, encryptor, System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(cs))
                        {
                            swEncrypt.Write(plainText); //Write all data to the stream.
                        }

                        cipherText = ms.ToArray();
                    }
                }
            }

            return cipherText;
        }

        public string Decrypt(byte[] cipherText)
        {
            string plainText = string.Empty;

            using (System.Security.Cryptography.AesManaged crypto = new System.Security.Cryptography.AesManaged())
            {
                // Create a decrytor to perform the stream transform.
                System.Security.Cryptography.ICryptoTransform decryptor = crypto.CreateDecryptor(this._key, this._iv);

                // Create the streams used for decryption.
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(cipherText))
                {
                    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(cs))
                        {
                            // Read the decrypted bytes from the decrypting stream and place them in a string.
                            plainText = sr.ReadToEnd();
                        }
                    }
                }
            }

            return plainText;
        }

    }
}
